﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Card.Web.Member
{
    public partial class Order : MemberBase
    {
        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DoInit();
        }

        protected override void OnInit(EventArgs e)
        {
            this.pager.BuildingUrl += new BAF.WebControls.BuildingUrlHander(pager_BuildingUrl);
            this.rptList.ItemDataBound += new RepeaterItemEventHandler(rptList_ItemDataBound);
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            base.OnInit(e);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string orderID = e.CommandArgument.ToString();
            if (string.Compare(e.CommandName, "PAY", true) == 0)
            {
                Response.Redirect(string.Format("ConfirmOrder.aspx?oid={0}", orderID));
            }
            else if (string.Compare(e.CommandName, "DELETE", true) == 0)
            {
                Entity.Order order = BLL.OrderManager.GetEntity(orderID, this.Passport);
                if (order != null && string.Compare(order.OrderStatusCode, CONST.ORDER_SATTUS.WAIT_PAY, true) == 0)
                {
                    List<Entity.OrderItem> lstItems = BLL.OrderItemManager.GetOrderItems(orderID, Passport);
                    BLL.OrderItemManager.DeleteByOrderID(orderID, Passport);
                    BLL.OrderManager.Delete(order.OrderID, this.Passport);
                    foreach (Entity.OrderItem item in lstItems)
                    {
                        if (string.Compare(item.CardType, CONST.CARD_TYPE.ORDER_CARD, true) == 0)
                        {
                            BLL.OrderCardManager.Delete(item.CardID, this.Passport);
                            BLL.OrderCardFieldManager.DeleteByOrderCardID(item.CardID, this.Passport);
                            BLL.OrderCardLogoManager.DeleteByOrderCardID(item.CardID, this.Passport);
                            Card.Common.CardWebUtility.DeleteOrderCardFiles(item.CardID, this.Passport);
                        }
                    }
                }
                

                Response.Redirect(string.Format("Order.aspx?i={0}", this.pager.PageIndex));
            }
            else if (string.Compare(e.CommandName, "DETAIL", true) == 0)
            {
                Response.Redirect(string.Format("OrderDetail.aspx?oid={0}", orderID));
            }
        }

        void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lbtnPay = e.Item.FindControl("lbtnPay") as LinkButton;
                LinkButton lbtnDelete = e.Item.FindControl("lbtnDelete") as LinkButton;
                DataRowView drv = e.Item.DataItem as DataRowView;
                if (string.Compare(CONST.ORDER_SATTUS.WAIT_PAY, drv["ORDER_STATUS_CODE"].ToString().Trim(), true) == 0)
                {
                    lbtnPay.Visible = true;
                    lbtnDelete.Visible = true;
                }
            }
        }

        string pager_BuildingUrl(int index)
        {
            return string.Format("order.aspx?i={0}", index);
        }
        #endregion

        #region

        private void DoInit()
        {
            Card.DataBase.Pagination pagination = new Card.DataBase.Pagination()
            {
                TableName = "[ORDER]",
                PageSize = this.pager.PageSize,
                OrderBy = "SUBMIT_DATE DESC",
                PageNumber = this.pager.PageIndex + 1,
                Condition = string.Format(" AND USER_ID={0}", this.Passport.UserID)
            };

            DataTable dtData = BLL.TemplateManager.Query(pagination);
            this.pager.RecordCount = pagination.RecordCount;
            this.rptList.DataSource = dtData;
            this.rptList.DataBind();
        }
        #endregion
    }
}
